﻿using System;
using System.Windows.Forms;

namespace RibbonEditor.Controls
{
    public partial class DoubleControl : UserControl, IDirtable
    {
        bool firstAccess = true;

        double? initialState = null;

        public bool IsDirty
        {
            set { if (value == false && this.txtText.Text.Length > 0) this.initialState = double.Parse(this.txtText.Text); this.isDirty = value; }
            get { return this.isDirty; }
        }

        private bool isDirty;

        public string Label
        {
            get
            {
                return this.lblLabel.Text;
            }
            set
            {
                this.lblLabel.Text = value;
            }
        }

        public double? Value
        {
            get
            {
                if (this.txtText.Text.Length > 0)
                {
                    return double.Parse(this.txtText.Text);
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (!firstAccess)
                {
                    if (initialState == null)
                        initialState = value;
                }
                else
                {
                    firstAccess = false;
                }

                this.txtText.Text = value.ToString();
            }
        }

        public bool IsRequired
        {
            get
            {
                return this.lblRequired.Visible;
            }
            set
            {
                this.lblRequired.Visible = value;
            }
        }

        bool isEditable = true;

        public bool IsEditable
        {
            get { return isEditable; }
            set { isEditable = value; }
        }

        public DoubleControl()
        {
            //this.txtText.TextChanged -= new System.EventHandler(this.txtText_TextChanged);

            InitializeComponent();

            this.txtText.Enabled = isEditable;
         
            this.txtText.TextChanged += new System.EventHandler(this.txtText_TextChanged);
        }

        private void txtText_TextChanged(object sender, EventArgs e)
        {
            if (txtText.Text.Length > 0)
            {
                double parsedDouble;

                if (double.TryParse(this.txtText.Text, out parsedDouble))
                {
                    this.Value = parsedDouble;
                    this.IsDirty = this.initialState != double.Parse(this.txtText.Text);
                }
                else
                {
                    MessageBox.Show(this, "Only double value can be written!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtText.Text = this.txtText.Text.Substring(0, this.txtText.Text.Length - 1);
                }
            }
        }

        public void ReinitValues()
        {
            this.txtText.Text = this.initialState.Value.ToString();
            this.isDirty = false;
        }
    }
}
